//
//  JXAboutUsController.m
//  JiaoX
//
//  Created by yinqy on 15/1/26.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXAboutUsController.h"
#import "HomeDataTool.h"
#import "JXAppInfo.h"

#define TITLE_FONT_SIZE [DimensUtils getScaleX:18]
#define VER_FONT_SIZE [DimensUtils getScaleX:15]

@interface JXAboutUsController ()
{
    UIScrollView *_scrollView;
    UILabel *_titleLabel;
    UILabel *_versionLabel;
    UILabel *_aboutLabel;
    UIImageView *_newImage;
    
    JXAppInfo *_appInfo;
}

@end

@implementation JXAboutUsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"关于娇羞"];
    [self.view setBackgroundColor:JXGlobalColorBg];
    [self initViews];
    [self checkAppInfo];
}

-(void)initViews{
    //先添加scrollview
    _scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_scrollView];
    
    
    _titleLabel = [[UILabel alloc]init];
    [_titleLabel setTextColor:[UIColor blackColor]];
    [_titleLabel setFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE]];
    [_titleLabel setText:@"娇羞-情趣助手"];
    CGSize titleSize = [_titleLabel.text sizeWithFont:_titleLabel.font];
    [_titleLabel setFrame:CGRectMake((JXScreenW-titleSize.width)/2, 30, titleSize.width, titleSize.height)];
    [_scrollView addSubview:_titleLabel];
    
    _versionLabel = [[UILabel alloc]init];
    [_versionLabel setTextColor:JXColor(77, 77, 77)];
    [_versionLabel setFont:[UIFont systemFontOfSize:VER_FONT_SIZE]];
    NSString *verStr = [NSString stringWithFormat:@"iOS V%@",[Utils getAppVersion]];
    [_versionLabel setText:verStr];
    CGSize verSize = [_versionLabel.text sizeWithFont:_versionLabel.font];
    [_versionLabel setFrame:CGRectMake((JXScreenW-verSize.width)/2, CGRectGetMaxY(_titleLabel.frame)+JXPublicInset, verSize.width, verSize.height)];
    [_scrollView addSubview:_versionLabel];
    
    _newImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"new"]];
    _newImage.x = CGRectGetMaxX(_versionLabel.frame)+JXPublicInset/2;
    _newImage.y = _versionLabel.y;
    _newImage.hidden = YES;
    [_scrollView addSubview:_newImage];
    
    _aboutLabel = [[UILabel alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_versionLabel.frame)+30, JXScreenW-JXPublicInset*2, 0)];
    [_aboutLabel setTextColor:JXColor(77, 77, 77)];
    [_aboutLabel setNumberOfLines:0];
    [_aboutLabel setFont:[UIFont systemFontOfSize:VER_FONT_SIZE]];
    [_scrollView addSubview:_aboutLabel];
    
    
}


-(void)checkAppInfo{
    [HomeDataTool appInfo:^(JXAppInfo *result) {
        if([Utils isBlankString:result.data]){
            //在这个地方填充数据
            CGSize aboutSize = [result.appAbout sizeWithFont:_aboutLabel.font constrainedToSize:CGSizeMake(_aboutLabel.width, MAXFLOAT)];
            [_aboutLabel setHeight:aboutSize.height];
            [_aboutLabel setText:result.appAbout];
            [_scrollView setContentSize:CGSizeMake(JXScreenW, CGRectGetMaxY(_aboutLabel.frame)+JXPublicInset)];
            
            if([result.appVersion compare:[Utils getAppVersion]]==NSOrderedDescending){
                _appInfo = result;
                //有新版
                _newImage.hidden = NO;
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:result.appContent message:@"" delegate: self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
                [alert show]; //显示退出对话框
            }else{
                //没有新版
                _newImage.hidden = YES;
            }
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
    }];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //前往appstre或者其他什么地方去更新
        //itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=942925113
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_appInfo.appUrl]];
    }
}

@end
